//
//  UILabelExtension.swift
//  YpfProject
//
//  Created by volantPig on 2021/5/1.
//

import UIKit

class UILabelExtension: NSObject {

}

extension UILabel {
    func labelConfig(textAlignment: NSTextAlignment, font: UIFont, textColor: UIColor, numberOfLines: Int = 1) {
        self.textAlignment = textAlignment
        self.font = font
        self.textColor = textColor
        self.lineBreakMode = NSLineBreakMode.byCharWrapping
        self.numberOfLines = numberOfLines
    }
    
    // 将文字内容划线以表删除
    /// 文字划线
    func markText() {
        //MARK: 当前方法不支持富文本划线
        if let currentStr = self.text , currentStr.count > 0 {
            let attributedStr = NSMutableAttributedString.init(string: currentStr)
            attributedStr.addAttributes([NSAttributedString.Key.font: self.font as Any, NSAttributedString.Key.foregroundColor: self.textColor as Any, NSAttributedString.Key.baselineOffset: 0, NSAttributedString.Key.strikethroughStyle: 1], range: NSRange.init(location: 0, length: currentStr.count))
            self.text = ""
            self.attributedText = attributedStr
        }
    }
 
    /// 文字 上下 行间距
    func addTextLineSpace(spaceHeight: CGFloat) {
        if let currentStr = self.text , currentStr.count > 0 {
            let attributedStr = NSMutableAttributedString.init(string: currentStr)
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineSpacing = spaceHeight
            attributedStr.addAttributes([NSAttributedString.Key.paragraphStyle: paragraphStyle], range: NSRange.init(location: 0, length: currentStr.count))
            self.text = ""
            self.attributedText = attributedStr
        }
    }
    
    /// 价格Label驼峰样式
    /// - Parameters:
    ///   - markFont: 金钱符号的大小 ps: 目前写死固定中文金币符号￥
    ///   - mantissaFont: 尾数文字大小，包含.在内后面的尾数，即1000.44的.44
    func priceString(_ markFont: UIFont,_ mantissaFont: UIFont){
        if let currentStr = self.text {
            
            if (currentStr.contains("￥") && currentStr.contains(".")) == false {
                return
            }
            
            let attributedStr = NSMutableAttributedString.init(string: currentStr)
            attributedStr.beginEditing()
            
            var firstNSRange: NSRange?
            var lastNSRange: NSRange?
            var middleNSRange: NSRange?
            if let firstRange = currentStr.range(of: "￥") {
                firstNSRange = NSRange.init(firstRange, in: currentStr)
                attributedStr.addAttributes([NSAttributedString.Key.font: markFont], range: firstNSRange!)
            }
            if let lastRange = currentStr.range(of: ".") {
                lastNSRange = NSRange.init(lastRange, in: currentStr)
                lastNSRange = NSRange.init(location: (lastNSRange?.location ?? 0), length: currentStr.count - (lastNSRange?.location ?? 0))
                attributedStr.addAttributes([NSAttributedString.Key.font: mantissaFont], range: lastNSRange!)
            }

            let middleLocation = (firstNSRange?.location ?? 0) + (firstNSRange?.length ?? 0)
            let middleLength = (lastNSRange?.location ?? 0) - middleLocation

            middleNSRange = NSRange.init(location: middleLocation , length: middleLength)

            attributedStr.addAttributes([NSAttributedString.Key.font: mantissaFont], range: middleNSRange!)
            attributedStr.endEditing()
            self.text = ""
            self.attributedText = attributedStr
        }
    }
    
    /// 最近文字富文本
    func appendAttributedString(appenStr: String, attrs: [NSAttributedString.Key : Any]) {

        var baseAttributedStr: NSMutableAttributedString?
//        var attributedDic: [NSAttributedString.Key : Any]?
        if let currentStr = self.text {
            baseAttributedStr = NSMutableAttributedString(string: currentStr)
        }
        if let currentAttributedStr = self.attributedText {
            baseAttributedStr = NSMutableAttributedString(attributedString: currentAttributedStr)
//            attributedDic = currentAttributedStr.attributes(at: 0, longestEffectiveRange: nil, in: NSRange(location: 0, length: currentAttributedStr.string.count))
        }
        guard let baseStr = baseAttributedStr else { return }
        
        var appendAttributedStr = NSMutableAttributedString(string: appenStr)
        appendAttributedStr.addAttributes(attrs, range: NSRange(location: 0, length: appenStr.count))
        baseStr.append(appendAttributedStr)
        
        self.attributedText = baseStr
    }
    
    /// 追加图片  ps:与文字大小相仿
    func appendImgV(_ imageInfo: Any,at index: Int = -1) {
        
        var image: UIImage?
        if type(of: imageInfo).self == String.self {
            print("文字")
            let imageName = imageInfo as! String
            image = UIImage.init(named: imageName)
        }
        
        if type(of: imageInfo).self == UIImage.self {
            print("图片")
            image = imageInfo as? UIImage
        }
        
        if type(of: imageInfo).self == UIImageView.self {
            print("imageView")
            let imgV = imageInfo as! UIImageView
            image = imgV.image
        }

        let attach = NSTextAttachment()
        attach.image = image
        //获取顶边距离 使其居中
        let paddingTop = self.font.lineHeight - self.font.pointSize
        attach.bounds = CGRect.init(x: 0, y: -paddingTop/2.0, width: self.font.ascender, height: self.font.ascender)
        let imageStr = NSAttributedString.init(attachment: attach)
        let attributeStr = NSMutableAttributedString.init(string: self.text ?? "")
        
        if index == -1 {
            attributeStr.append(imageStr)
        } else {
            attributeStr.insert(imageStr, at: index)
        }
        self.attributedText = attributeStr
    }

    
}
